/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundless.managedbeans;

import boundless.calculator.TeamStatCalculator;
import java.util.List;
import javax.inject.Named;
import javax.enterprise.context.Dependent;
import boundless.entities.*;
import boundless.ejb.*;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.inject.Inject;

/**
 *
 * @author hendrikdekock
 */
@Named(value = "scheduleBean")
@Dependent
public class ScheduleBean {


    @Inject
    Account curUser;
    
    @EJB
    RequestBean rb;
    
    String error;
    TeamStatCalculator calc;
    /**
     * The schedule the user is viewing
     */
    public List<Gamehistory> schedule;
    
    Gamehistory viewing;
    
    /**
     * Creates a new instance of ScheduleBean
     */
    public ScheduleBean() {
        calc = new TeamStatCalculator(rb);
    }
    
    public void setRequestBean(RequestBean rb) {
        this.rb = rb;
        calc.setReqBean(rb);
    }
    
    public void setCurUser(Account acc) {
        curUser = acc;
    }
    
    /**
     * Updates the Game History for this team
     */
    public void viewGameHistoryForMyTeam() {
        
    }
    
    /**
     * Updates the Game History for this user's league
     */
    public void viewGameHistoryForMyLeague() {
        
    }
    
    public void selectGameHistory(Gamehistory gh) {
        viewing = gh;
    }
    
    /**
     * Updates the Game History for another team in the league
     * @param t 
     */
    public void viewGameHistoryForTeam(Fantasyteams t) {
        
    }
    
    /**
     * Plays the selected game and updates it for the result
     * @param gh : The game to play
     */
    public void playGame(Gamehistory gh) {
        if(gh.getScore1() == 0 && gh.getScore2() == 0) {
           ArrayList<Player> team1Players = new ArrayList<Player>();
           ArrayList<Player> team2Players = new ArrayList<Player>();
           Fantasyteams team1 = gh.getFantasyteams();
           Fantasyteams team2 = gh.getFantasyteams();
           
           for (Playerteam p : rb.getActivePlayersOfTeam(team1)) {
               team1Players.add(p.getPlayer());
           }
           for (Playerteam p : rb.getActivePlayersOfTeam(team2)) {
               team2Players.add(p.getPlayer());
           }
           int score1 = calc.calculateTeamPoints(team1Players);
           int score2 = calc.calculateTeamPoints(team2Players);
           if (score1 == score2) {
               team1.setTie(team1.getTie() + 1);
               team2.setTie(team2.getTie() + 1);
           }
           else if (score1 > score2) {
            team1.setWin(team1.getWin() + 1);
            team2.setLose(team2.getLose() + 1);
            }
           else if (score1 < score2) {
               team1.setLose(team1.getLose() + 1);
               team2.setWin(team2.getWin() + 1);
           }
           gh.setScore1(score1);
           gh.setScore2(score2);
               
        }
        else {
            error = "Game already played";
        }
        
    }
}
